﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MDIDemo.Windows;

namespace MDIDemo.Modules
{
    public partial class ChuyenKhoanHD : DevExpress.XtraEditors.XtraForm
    {
        long no = -1;
        string sobt = "";
        string sErr = "";
        int idThem = 57;
        int idSua = 58;
        int idXoa = 59;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION) || frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
            }
        }
        public ChuyenKhoanHD()
        {
            InitializeComponent();

        }

        private void cboLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshForm("");
        }

        private void ChuyenKhoanHD_Load(object sender, EventArgs e)
        {
            LoadLoaiBT();
            EnabledForm(false);
            SetPermission();
        }

        private void LoadLoaiBT()
        {
            BUS.CommonControl ctr = new BUS.CommonControl();
            DataTable dt = ctr.executeSelectQuery("SELECT * FROM LOAIBT WHERE MA like 'CK%'");
            cboLoai.DataSource = dt;
            cboLoai.SelectedIndex = 0;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            frmViewChuyenKhoanHD frm = new frmViewChuyenKhoanHD();
            frm.LoaiBT = cboLoai.SelectedValue;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                DTO.BAOCAOTHUEInfo inf = new DTO.BAOCAOTHUEInfo(frm.ReturnRow);
                SetData(inf);
            }
        }

        private void SetData(DTO.BAOCAOTHUEInfo inf)
        {
            no = inf.ID;
            txtNgayGD.EditValue = inf.NGAYGD;
            txtNoiDung.Text = inf.HANGHOA;
            txtSoBT.Text = inf.SOBT;
            txtDoanhSo.EditValue = inf.DOANHSO;
            BUS.HACHTOANControl htCtr = new BUS.HACHTOANControl();
            DataTable dt = htCtr.GetByNo(inf.ID, ref sErr);
            string type = cboLoai.SelectedValue;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DTO.HACHTOANInfo htInf = new DTO.HACHTOANInfo(dt.Rows[i]);
                if (type == "CK-000")
                {
                    if (i == 0)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK2.Text = htInf.TKCO;
                        txtTKValue1.EditValue = txtTKValue2.EditValue = htInf.SOTIEN;
                    }
                }
                else if (type == "CK-001")
                {
                    if (i == 0)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK2.Text = htInf.TKCO;
                        txtTKValue1.EditValue = inf.DOANHSO;
                        txtTKValue2.EditValue = htInf.SOTIEN;

                    }
                    else if (i == 1)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK3.Text = htInf.TKCO;
                        txtTKValue3.EditValue = htInf.SOTIEN;

                    }
                    else if (i == 2)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK4.Text = htInf.TKCO;
                        txtTKValue4.EditValue = htInf.SOTIEN;

                    }
                }
                else if (type == "CK-002")
                {
                    if (i == 0)
                    {
                        txtTK4.Text = htInf.TKCO;
                        txtTK1.Text = htInf.TKNO;
                        txtTKValue4.EditValue = inf.DOANHSO;
                        txtTKValue1.EditValue = htInf.SOTIEN;

                    }
                    else if (i == 1)
                    {
                        txtTK4.Text = htInf.TKCO;
                        txtTK2.Text = htInf.TKNO;
                        txtTKValue2.EditValue = htInf.SOTIEN;

                    }
                    else if (i == 2)
                    {
                        txtTK4.Text = htInf.TKCO;
                        txtTK3.Text = htInf.TKNO;
                        txtTKValue3.EditValue = htInf.SOTIEN;
                    }
                }
            }
            //txtTK1.Text = p;
            //txtTK2.Text = p;
            //txtTK3.Text = p;
            //txtTK4.Text = p;
            //txtTKDesc1.Text = p;
            //txtTKDesc2.Text = p;
            //txtTKDesc3.Text = p;
            //txtTKDesc4.Text = p;
            //txtTKValue1.EditValue = 0;
            //txtTKValue2.EditValue = 0;
            //txtTKValue3.EditValue = 0;
            //txtTKValue4.EditValue = 0;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            DataSet _ds = new DataSet();
            if (no == -1)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }

            string query = @"exec procBAOCAOTHUE_baocaothue @ID={0}";
            query = string.Format(query, no);
            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);
            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            string reportCode = "CK";// cboLoai.SelectedValue;
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = this.Parent.Parent as frmMain;
                main.ShowReport(_ds, reportCode, "Báo cáo chuyển khoản hóa đơn");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefreshForm("");
            EnabledForm(true);
        }

        private void EnabledForm(bool p)
        {
            txtNgayGD.Enabled = p;
            txtNoiDung.Enabled = p;
            txtSoBT.Enabled = p;
            txtDoanhSo.Enabled = p;
            txtTK1.Enabled = p;
            txtTK2.Enabled = p;
            txtTK3.Enabled = p;
            txtTK4.Enabled = p;
            txtTKDesc1.Enabled = p;
            txtTKDesc2.Enabled = p;
            txtTKDesc3.Enabled = p;
            txtTKDesc4.Enabled = p;
            btnTK1.Enabled = p;
            btnTK2.Enabled = p;

            if (frmMain.__khoaSo)
            {
                btnXoa.Enabled = btnThem.Enabled = btnXoa.Enabled = btnSua.Enabled = false;
            }
            else
            {
                btnXoa.Enabled = btnThem.Enabled = btnXoa.Enabled = btnSua.Enabled = !p;
            }
            btnLuu.Enabled = p;


            btnBoQua.Enabled = p;
            btnTimKiem.Enabled = !p;
            btnIn.Enabled = !p;
            cboLoai.Enabled = !p;

            string type = cboLoai.SelectedValue;
            if (type == "CK-000" && p)
            {
                txtTKValue1.Enabled = !p;
                txtTKValue2.Enabled = !p;
            }
            else
            {
                txtTKValue1.Enabled = p;
                txtTKValue2.Enabled = p;
            }
            if (type == "CK-001" && p)
            {
                txtTKValue1.Enabled = !p;
            }
            if (type == "CK-002" && p)
                txtTKValue4.Enabled = !p;
            btnTK3.Enabled = p & type != "CK-000";
            txtTK3.Enabled = p & type != "CK-000";
            txtTKValue3.Enabled = p & type != "CK-000";
            btnTK4.Enabled = p & type != "CK-000";
            txtTK4.Enabled = p & type != "CK-000";
            txtTKValue4.Enabled = p & type != "CK-000";
        }

        private void RefreshForm(string p)
        {
            no = -1;
            txtNgayGD.EditValue = frmMain.__ngayMoSo;
            txtNoiDung.Text = p;
            txtSoBT.Text = NXHoaDon.GetNewSoBT();
            txtDoanhSo.EditValue = 0;
            txtTK1.Text = p;
            txtTK2.Text = p;
            txtTK3.Text = p;
            txtTK4.Text = p;
            txtTKDesc1.Text = p;
            txtTKDesc2.Text = p;
            txtTKDesc3.Text = p;
            txtTKDesc4.Text = p;
            txtTKValue1.EditValue = 0;
            txtTKValue2.EditValue = 0;
            txtTKValue3.EditValue = 0;
            txtTKValue4.EditValue = 0;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (no != -1 && XtraMessageBox.Show("Bạn có thực sự muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                BUS.BAOCAOTHUEControl bcCtr = new BUS.BAOCAOTHUEControl();
                sErr = bcCtr.Delete(no);
                if (sErr == "")
                {
                    BUS.HACHTOANControl htCtr = new BUS.HACHTOANControl();
                    htCtr.Delete(no, ref sErr);
                    EnabledForm(false);
                    RefreshForm("");
                }
                else XtraMessageBox.Show(sErr);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if ((txtTK1.Enabled & txtTK1.Text == "" & txtTKValue1.EditValue.ToString() != "0") ||
                (txtTK2.Enabled & txtTK2.Text == "" & txtTKValue2.EditValue.ToString() != "0") ||
                (txtTK3.Enabled & txtTK3.Text == "" & txtTKValue3.EditValue.ToString() != "0") ||
                (txtTK4.Enabled & txtTK4.Text == "" & txtTKValue4.EditValue.ToString() != "0"))
            {

                XtraMessageBox.Show("Vui lòng nhập thêm thông tin chuyển khoản!");
                return;

            }
            if ((cboLoai.SelectedValue == "CK-000" && txtTKValue1.EditValue != txtTKValue2.EditValue) ||
                (cboLoai.SelectedValue == "CK-001" && Convert.ToDouble(txtTKValue2.EditValue) + Convert.ToDouble(txtTKValue3.EditValue) + Convert.ToDouble(txtTKValue4.EditValue) != Convert.ToDouble(txtTKValue1.EditValue)) ||
                (cboLoai.SelectedValue == "CK-002" && Convert.ToDouble(txtTKValue1.EditValue) + Convert.ToDouble(txtTKValue2.EditValue) + Convert.ToDouble(txtTKValue3.EditValue) != Convert.ToDouble(txtTKValue4.EditValue)))
            {
                XtraMessageBox.Show("Dữ liệu đưa vào không cân, vui lòng nhập lại!");
                return;
            }

            BUS.BAOCAOTHUEControl cbCtr = new BUS.BAOCAOTHUEControl();
            DTO.BAOCAOTHUEInfo bcInf = GetData();
            if (bcInf.ID == -1)
            {
                bcInf.SOBT = NXHoaDon.GetNewSoBT();
                no = bcInf.ID = cbCtr.Add(bcInf, ref sErr);
                txtSoBT.Text = bcInf.SOBT;
            }
            else sErr = cbCtr.Update(bcInf);
            if (sErr == "")
            {
                BUS.HACHTOANControl htCtr = new BUS.HACHTOANControl();
                htCtr.Delete(bcInf.ID, ref sErr);
                string type = cboLoai.SelectedValue;
                if (type == "CK-000")
                {
                    DTO.HACHTOANInfo htInf1 = new DTO.HACHTOANInfo(bcInf.ID, 1, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK3.Text, Convert.ToDouble(txtTKValue1.EditValue), "");
                    DTO.HACHTOANInfo htInf2 = new DTO.HACHTOANInfo(bcInf.ID, 2, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK2.Text, txtTK3.Text, Convert.ToDouble(txtTKValue2.EditValue), "");
                    htCtr.Add(htInf1, ref sErr);
                    htCtr.Add(htInf2, ref sErr);
                }
                else if (type == "CK-001")
                {
                    DTO.HACHTOANInfo htInf1 = new DTO.HACHTOANInfo(bcInf.ID, 1, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK2.Text, Convert.ToDouble(txtTKValue2.EditValue), "");
                    DTO.HACHTOANInfo htInf2 = new DTO.HACHTOANInfo(bcInf.ID, 2, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK3.Text, Convert.ToDouble(txtTKValue3.EditValue), "");
                    htCtr.Add(htInf1, ref sErr);
                    htCtr.Add(htInf2, ref sErr);
                }
                else
                {
                    DTO.HACHTOANInfo htInf1 = new DTO.HACHTOANInfo(bcInf.ID, 1, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK2.Text, Convert.ToDouble(txtTKValue1.EditValue), "");
                    htCtr.Add(htInf1, ref sErr);
                }
                EnabledForm(false);
            }
            else XtraMessageBox.Show(sErr);

        }

        private DTO.BAOCAOTHUEInfo GetData()
        {
            DTO.BAOCAOTHUEInfo ret = new DTO.BAOCAOTHUEInfo();
            ret.ID = no;
            ret.SOBT = txtSoBT.Text;
            ret.BANRA = true;
            ret.CREATEBY = frmMain.__userID;
            ret.MODIFIEDBY = frmMain.__userID;
            ret.CREATEDATE = DateTime.Now;
            ret.MODIFIEDDATE = DateTime.Now;
            ret.DOANHSO = Convert.ToDouble(txtDoanhSo.EditValue);
            ret.HANGHOA = ret.GHICHU = txtNoiDung.Text;
            ret.ID = no;
            ret.LOAIBT = cboLoai.SelectedValue;
            ret.NGAYGD = txtNgayGD.EditValue != null ? Convert.ToDateTime(txtNgayGD.EditValue) : frmMain.__ngayMoSo;
            ret.NGAYPS = ret.NGAYGD;


            return ret;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            EnabledForm(false);
        }

        private void txtDoanhSo_Validated(object sender, EventArgs e)
        {
            if (cboLoai.SelectedValue != null)
            {
                string type = cboLoai.SelectedValue;
                if (type == "CK-000" || type == "CK-001")
                    txtTKValue1.EditValue = txtDoanhSo.EditValue;
                if (type == "CK-002")
                    txtTKValue4.EditValue = txtDoanhSo.EditValue;
            }
        }

        private void btnTK1_Click(object sender, EventArgs e)
        {
            frmViewTaiKhoan frm = new frmViewTaiKhoan();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtTK1.Text = frm.ReturnRow[DTO.ACC_SYSInfo.Field.TKC2.ToString()].ToString();
            }
        }

        private void btnTK2_Click(object sender, EventArgs e)
        {
            frmViewTaiKhoan frm = new frmViewTaiKhoan();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtTK2.Text = frm.ReturnRow[DTO.ACC_SYSInfo.Field.TKC2.ToString()].ToString();
            }
        }

        private void btnTK3_Click(object sender, EventArgs e)
        {
            frmViewTaiKhoan frm = new frmViewTaiKhoan();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtTK3.Text = frm.ReturnRow[DTO.ACC_SYSInfo.Field.TKC2.ToString()].ToString();
            }
        }

        private void btnTK4_Click(object sender, EventArgs e)
        {
            frmViewTaiKhoan frm = new frmViewTaiKhoan();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtTK4.Text = frm.ReturnRow[DTO.ACC_SYSInfo.Field.TKC2.ToString()].ToString();
            }
        }

        private void ChuyenKhoanHD_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
               
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
              
            }
        }


    }
}